package com.shigu.admin.manager.actions;

import com.alibaba.fastjson.JSONObject;
import com.shigu.admin.manager.exception.UnhandleException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 类名：DubboMonitor
 * 类路径：com.shigu.admin.manager.actions.DubboMonitor
 * 创建者：王浩翔
 * 创建时间：2017-09-07 17:46
 * 项目：sites.admin
 * 描述：监控dubbo服务
 */
@Controller
public class DubboMonitor {

    @RequestMapping("adminDubboMonitor/{minNum}/{service}")
    @ResponseBody
    public JSONObject monitorDubbo(@PathVariable("service") String service, @PathVariable("minNum") Integer minNum) throws Throwable {
        try {
            Document document = Jsoup.connect("http://so.571xz.com:30005/providers.html").data("service", service.replaceAll("_",".").trim()).ignoreContentType(true).get();
            Elements tables = document.select("table");
            Elements columns = tables.get(1).select("tbody");
            Integer activeNum = columns.select("tr").size();
            if (activeNum <= minNum) {
                throw new Exception();
            }
            JSONObject result = new JSONObject();
            result.put("activeProvider", activeNum);
            return result;
        } catch (Exception e) {
            throw new UnhandleException();
        }
    }

}
